
$(function() {
	$("input[id^='machine_s']:visible").autocomplete(Test.cnc);
	
	$("input[id^='customer_t']:visible").autocomplete(Test.customer_code);
	
	$("select[id^='day_t']:visible").change(function() {
		var s = $(this).val();
		
		day_t(s);
	});
	
	$("select[id^='shift_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	});
});

function day_t(s) {
	
	var date1, date2;
	
	switch(parseInt(s)) {
		case 1:
			date2 = new Date();
			date1 = new Date();
			date1.setDate(date1.getDate() - 1);
			
			break;
		case 2:
			date1 = new Date();
			date2 = new Date();
			date2.setDate(date2.getDate() + 1);
			break;
		case 3:
			date1 = new Date();
			date1.setDate(date1.getDate() + 1);
			
			date2 = new Date();
			date2.setDate(date2.getDate() + 2);
			break;
		case 4:
			var now = new Date();
			
		    date2 = new Date(now - (now.getDay()-1) * 86400000);
		    
		    date1 = new Date();
		    date1.setDate(date2.getDate() - 7);
			
			break;
		case 5:
			var now = new Date();
		    var date1 = new Date(now - (now.getDay()-1) * 86400000);
		    
		    date2 = new Date();
		    date2.setDate(date1.getDate() + 7);
			
			break;
		default:
			
	}
	
	var t1 = $.dateFormat.date(date1, "yyyy-MM-dd");
	var t2 = $.dateFormat.date(date2, "yyyy-MM-dd");
	// alert(t2);
	
	$("input[id^='date1_']:visible").val(t1);
	$("input[id^='date2_']:visible").val(t2);
}

var Produce = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				body.machine = form.machine.value;
				body.shift = form.shift.value;
				
				body.mold = form.mold.value;
				body.output = form.output.value;
				body.quantity = form.quantity.value;
				
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.status = form.status.value;

				$(this).dialog("close");

				$.post('car.php?do=Produce.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'car.php?do=Produce.edit&id=' + id + p2;
		var c2 = 'car.php?do=Produce.view&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'Produce.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x400'];
		
		var c4 = 'car.php?do=Maintain.add&id=' + id;
		var c5 = 'car.php?do=Produce.status&id=' + id;

		var a4 = ['车辆服务', 'url', 'Produce.dialog_maintain', '600x400'];
		var a5 = ['恢复正常', 'url', 'Produce.list_ok'];

		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c1 = 'car.php?do=Produce.upload&id=' + id;
		var a1 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var c2 = 'car.php?do=Produce.copy&id=' + id;
		var a2 = ['复制信息', 'url', 'Produce.dialog_copy_', '650x400'];
		
		var ss = 'car.php?do=Produce.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Produce.dialog_ss', '400x200'];
		
		s += ", " + c1 + ", " + c2 + ", " + ss;
		
		a.push(a1);
		a.push(a2);
		
		a.push(bb);
		
		return popup(e, a, s);
	}, 

	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Produce.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'car.php?do=Produce.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	}, 

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Produce.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						body.machine = form.machine.value;
						body.shift = form.shift.value;
						
						body.mold = form.mold.value;
						body.output = form.output.value;
						body.quantity = form.quantity.value;
						
						body.name = form.name.value;
						body.content = form.content.value;

						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'car.php?do=Produce.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				body.machine = form.machine.value;
				body.shift = form.shift.value;
				
				body.mold = form.mold.value;
				body.output = form.output.value;
				body.quantity = form.quantity.value;
				
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.status = form.status.value;

				$(this).dialog("close");

				var url = 'car.php?do=Produce.addPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_batch: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				
				body.shift = form.shift.value;
				body.date = form.date.value;
				body.content = form.content.value;
				
				// body.item = data;
				body.item = $.toJSON(data);

				$(this).dialog("close");

				$.post('car.php?do=Produce.batchPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	download: function (key) {
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'car.php?do=Produce.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'car.php?do=Produce.upload&id=' + id;
		
		var url = 'car.php?do=Produce.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},

	dialog_maintain: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Produce.audit&item=75', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"添加": function() {
						if (!confirm("确认是否添加？")) {
							return false;
						}
						
						var form = $("#form_add")[0];

						var body = { };
						body.id = form.id.value;
						body.title = form.title.value;
						body.maintain = form.maintain.value;

						body.content = form.content.value;
						body.cost = form.cost.value;
						
						$(this).dialog("close");

						$.post('car.php?do=Maintain.addPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'car.php?do=Produce.delete' + p2;		
		var c2 = 'car.php?do=Produce.update' + p2;
		var c3 = 'car.php?do=Produce.batch_copy' + p2;
		var c4 = 'car.php?do=Produce.batch_view' + p2;
		
		var c9 = 'car.php?do=Produce.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['批量更新', 'url', 'Produce.dialog_update', '750x450'];
		var a3 = ['批量复制', 'url', 'Produce.dialog_batch_copy', '750x450'];
		var a4 = ['批量打印', 'url', 'Produce.dialog_batch_view', '750x450'];
		
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c9;

		return popup(e, [a1, a2, a3, a4, a9], s);
	},
	
	dialog_update: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Produce.audit&item=152', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						
						var body = {};
						
						body.shift = form.shift.value;
						body.date = form.date.value;
						body.content = form.content.value;					
						body.status = form.status.value;
						
						// body.item = data;
						body.item = $.toJSON(data);
						
						var url = 'car.php?do=Produce.updatePost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	dialog_batch_copy: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Produce.audit&item=152', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						
						var body = {};
						
						body.shift = form.shift.value;
						body.date = form.date.value;
						body.content = form.content.value;					
						// body.status = form.status.value;
						
						// body.item = data;
						body.item = $.toJSON(data);
						
						var url = 'car.php?do=Produce.batchPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	dialog_batch_view: function (url, title, xy) {
		var result = Common.result();	
		var c = new Array();
		$("input[name^='c_']").each(function() {
			if ($(this).attr("checked")) {
				var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
				c.push(s);
			}
		});
		
		var body = c.join("&");
		
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},					
			
			"打印": function() { 
				$.jPrintArea("#dialog_view");
			}
		});

		var view = '#dialog_view';
		$.get(url, body, function(data) {
			$(view).html(data);
		});

		var regex = /^(\d+)(x)(\d+)$/;
		if (regex.test(xy)) {
			var cc = xy.split("x");
			var width = parseInt(cc[0]);
			var height = parseInt(cc[1]);
			$(view).dialog('option', 'height', height);
		} else {
			var width = parseInt(xy);
		}

		$(view).dialog('option', 'title', title);
		$(view).dialog('option', 'width', width);
		$(view).dialog('open');
	},
	
	_cc: function () {
		var c = new Array();
		$("input[name^='b_']").each(function() {
			if ($(this).attr("checked")) {
				c.push($(this).val());
			}
		});
		
		return c;
	},
	
	ext_x: function(form) {
		
		var ret = {};	
		
		ret.park = form.park.value;
		ret.cpxh = form.cpxh.value;
		ret.nsr = form.nsr.value;
		ret.fdjh = form.fdjh.value;
		ret.cjh = form.cjh.value;
		
		return $.toJSON(ret);
	}

};
